package graphisme;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import player.Player;

public class Home extends JFrame implements ActionListener, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 430251483396578518L;
	Container con = null;
	JPanel panelBgImg;
	JTextField txtName;

	public Home() {
		//window's title
		setTitle("Alix & Long's super game");
		con = getContentPane();

		con.setLayout(null);
		// background image
		ImageIcon imh = new ImageIcon("src/graphisme/Images/pac_man.jpeg");
		setSize(imh.getIconWidth(), imh.getIconHeight());

		panelBgImg = new JPanel() {
			private static final long serialVersionUID = 3399618330989994159L;

			public void paintComponent(Graphics g) {
				Image img = new ImageIcon("src/graphisme/Images/pac_man.jpeg")
						.getImage();
				Dimension size = new Dimension(img.getWidth(null),
						img.getHeight(null));
				setPreferredSize(size);
				setMinimumSize(size);
				setMaximumSize(size);
				setSize(size);
				setLayout(null);
				g.drawImage(img, 0, 0, null);
			}
		};

		con.add(panelBgImg);
		panelBgImg.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());

		GridBagLayout layout = new GridBagLayout();

		JPanel panelContent = new JPanel(layout);
		GridBagConstraints gc = new GridBagConstraints();

		gc.insets = new Insets(3, 3, 3, 3);
		gc.gridx = 1;
		gc.gridy = 1;

		JLabel label = new JLabel("UserName: ", JLabel.LEFT);
		panelContent.add(label, gc);

		gc.gridx = 2;
		gc.gridy = 1;

		this.txtName = new JTextField(10);
		panelContent.add(txtName, gc);

		gc.insets = new Insets(3, 3, 3, 3);
		gc.gridx = 1;
		gc.gridy = 2;
		gc.gridwidth = 2;

		JButton btn = new JButton("Login");
		btn.addActionListener(this);
		panelContent.add(btn, gc);
		panelContent.setBackground(Color.GRAY);
		panelContent.setBorder(new LineBorder(Color.WHITE));

		panelBgImg.add(panelContent);

		panelBgImg.setLayout(new FlowLayout(FlowLayout.CENTER, 150, 200));

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(true);
	}

	public static void main(String[] args) {
		new Home().setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// if pseudo is not empty
		// game menu appears
		if (!txtName.getText().equals("")) {
			Object[] possibilities = { "Rock-Paper-Scissors-Lizard-Spock",
					"Checkers" };
			String gameToPlay = (String) JOptionPane.showInputDialog(null,
					"Select the game you want to play",
					"Which game do you want to play ?", JOptionPane.NO_OPTION,
					null, possibilities, "Rock-Paper-Scissors-Lizard-Spock");
			// if player doesn't click on cancel or close the game menu by clicking on the red cross
			if (gameToPlay != null) {
				// launch the player
				new Thread(new Player(txtName.getText(), gameToPlay)).start();
				this.setVisible(false);
			}
		}
	}
}
